﻿using System;
using System.Windows;
using System.Windows.Controls;
using Pixster.Slideshow.BusinessObjects;

namespace Pixster.Slideshow
{
    /// <summary>
    /// Base class which all photo hosts in the slide show must inherit.
    /// </summary>
    public abstract class PhotoHostBase : Control
    {
        /// <summary>
        /// Dependency property for the photo property.
        /// </summary>
        public static readonly DependencyProperty PhotoProperty =
            DependencyProperty.Register("Photo", typeof(Photo), typeof(PhotoHostBase),
                new PropertyMetadata(new PropertyChangedCallback(OnPhotoChanged)));

        protected Image _image;

        /// <summary>
        /// Gets, sets the Photo property.
        /// </summary>
        public Photo Photo
        {
            get { return this.GetValue(PhotoProperty) as Photo; }
            set { this.SetValue(PhotoProperty, value); }
        }

        protected virtual void UpdateContent()
        {
            if (this.Photo == null)
                return;

            Dispatcher.BeginInvoke(new Action(this.DoUpdateContent));
        }

        /// <summary>
        /// Handle the work for updating the image source.
        /// </summary>
        protected abstract void DoUpdateContent();

        private static void OnPhotoChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((PhotoHostBase)sender).DoUpdateContent();
        }
    }
}